package com.lagou.sqlSession;

import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;
//import com.sun.javadoc.ParameterizedType;

import java.lang.reflect.*;
import java.util.List;

public class DefaultSqlSession implements SqlSession {

    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <E> List<E> selectList(String statementId, Object... param) throws Exception {
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        List<Object> list = simpleExecutor.query(configuration, configuration.getStatementMap().get(statementId), param);
        return (List<E>) list;
    }

    @Override
    public <T> T selectOne(String statementId, Object... param) throws Exception {
        List<Object> objects = selectList(statementId, param);
        if (objects.size()==1){
            return (T) objects.get(0);
        }else {
            throw  new RuntimeException("返回结果为空或者多条");
        }
    }

    /**
     * 除了select，其他的都是一样的执行方式
     * @param statementId
     * @param param
     * @return
     */
    @Override
    public int insertUpdateDelete(String statementId, Object... param) throws Exception {
        MappedStatement mappedStatement = configuration.getStatementMap().get(statementId);
        String key = mappedStatement.getKey();
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        int result = 0;
        if (!"select".equals(key)){
            result = simpleExecutor.update(configuration, mappedStatement, param);
        }else {
            List<Object> query = simpleExecutor.query(configuration, mappedStatement, param);
            return (int)query.get(0);//如果数据类型转化一场就是sql写的有问题
        }
        return result;
    }



    @Override
    public <E> E getMapper(Class<?> typeClass) {
        Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{typeClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                // 底层都还是去执行JDBC代码 //根据不同情况，来调用selctList或者selectOne
                // 准备参数 1：statmentid :sql语句的唯一标识：namespace.id= 接口全限定名.方法名
                // 方法名：findAll
                String methodName = method.getName();
                String className = method.getDeclaringClass().getName();

                String statementId = className+"."+methodName;
                if (method.getReturnType() == Integer.class){

                }
                // 准备参数2：params:args
                // 获取被调用方法的返回值类型
                Type genericReturnType = method.getGenericReturnType();
                // 判断是否进行了 泛型类型参数化
                if(genericReturnType instanceof ParameterizedType){
                    List<Object> objects = selectList(statementId, args);
                    return objects;
                }else if(method.getReturnType() == Integer.class||method.getReturnType() == int.class) {
                    return insertUpdateDelete(statementId, args);
                }

                return selectOne(statementId,args);
            }
        });
        return (E) proxyInstance;
    }


}
